package org.example.controller.backup;

import cn.hutool.core.bean.BeanUtil;
import org.example.common.result.ResponseData;
import org.example.flowable.handler.InstanceHandler;
import org.example.flowable.handler.TaskQueryHandler;
import org.example.repository.model.TaskVO;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 流程实例相关
 * </p>
 *
 * @author liuxz
 * @since 2019-08-20
 */
@RestController
@RequestMapping("api/flow/instance")
public class InstanceController {
    protected static Logger logger = LoggerFactory.getLogger(InstanceHandler.class);
    @Autowired
    private InstanceHandler instanceHandler;
    @Autowired
    private TaskQueryHandler taskQueryHandler;

    @RequestMapping(value = "/startByKey", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData startByName(String name, String tenantId, @RequestBody Map<String, Object> variables) {
        ProcessInstance pi = instanceHandler.startProcessInstanceByKeyAndTenantId(name, tenantId, variables);
        List<Task> list = taskQueryHandler.processInstanceId4Multi(pi.getProcessInstanceId());
        List<TaskVO> taskVO = new ArrayList<>();//BeanUtils.copyList(list, TaskVO.class);
        list.forEach(item->{
            taskVO.add(BeanUtil.copyProperties(item,TaskVO.class));
        });
        return ResponseData.success(taskVO);
    }


    @RequestMapping(value = "/startById", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData<TaskVO> startById(String id, @RequestBody Map<String, Object> variables) {
        ProcessInstance pi = instanceHandler.startProcessInstanceById(id, variables);
        Task task = taskQueryHandler.processInstanceId(pi.getProcessInstanceId());
        TaskVO taskVO = BeanUtil.copyProperties(task,TaskVO.class);
        return ResponseData.success(taskVO);
    }


    @RequestMapping(value = "/startAndExecute", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData startAndExecute(String name, String tenantId, String userId, @RequestBody Map<String, Object> variables) throws Exception {
        Map<String, Object> map = instanceHandler.startInstanceAndExecuteFirstTask(name, tenantId, userId, variables);
        return ResponseData.success(map);
    }


    @RequestMapping(value = "/startExecuteAndSetActor", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData startAndExecuteAndSetActor(String name, String tenantId, String userId,
                                                   @RequestBody Map<String, Object> variables) {
        Map<String, Object> map = instanceHandler.startInstanceAndExecuteFirstTask(name, tenantId, userId, variables);
        return ResponseData.success(map);
    }


    @RequestMapping(value = "/suspendById", method = RequestMethod.GET)
    @ResponseBody
    public ResponseData suspendById(String processInstanceId) throws Exception {
        instanceHandler.suspendProcessInstanceById(processInstanceId);
        return ResponseData.success("流程实例挂起成功");
    }


    @RequestMapping(value = "/activateById", method = RequestMethod.GET)
    @ResponseBody
    public ResponseData activateById(String processInstanceId) throws Exception {
        instanceHandler.activateProcessInstanceById(processInstanceId);
        return ResponseData.success("激活流程实例");
    }


    @RequestMapping(value = "/addMultiInstanceExecution", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData addMultiInstanceExecution(String activityDefId, String instanceId, @RequestBody Map<String, Object> variables) {
        instanceHandler.addMultiInstanceExecution(activityDefId, instanceId, variables);
        return ResponseData.success("加签成功");
    }

    @RequestMapping(value = "/deleteMultiInstanceExecution", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData deleteMultiInstanceExecution(String currentChildExecutionId, boolean flag) {
        instanceHandler.deleteMultiInstanceExecution(currentChildExecutionId, flag);
        return ResponseData.success("减签成功");
    }
}

